package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.databaseobject.ModuleDo;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;

/**
 * 首页固定4个主要模块
 */
public class HomeMainAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private DrawableRequestBuilder<String> requestBuilder;
    private ArrayList<ModuleDo> arrayList;

    public HomeMainAdapter(Context context, DrawableRequestBuilder<String> requestBuilder, ArrayList<ModuleDo> arrayList) {
        layoutInflater = LayoutInflater.from(context);
        this.requestBuilder = requestBuilder;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeMain homeMain;
        if (convertView == null) {
            homeMain = new HomeMain();
            convertView = layoutInflater.inflate(R.layout.item_home_main, parent, false);
            homeMain.img_home_main = (ImageView) convertView.findViewById(R.id.img_home_main);
            homeMain.atv_home_main = (AppCompatTextView) convertView.findViewById(R.id.atv_home_main);
            convertView.setTag(homeMain);
        } else {
            homeMain = (HomeMain) convertView.getTag();
        }
        final ModuleDo moduleDo = arrayList.get(position);
        GlideLoad.load(new GlideLoad.Builder(requestBuilder, moduleDo.getIcon())
                .placeHolder(R.drawable.ic_home_module)
                .error(R.drawable.ic_home_module)
                .into(homeMain.img_home_main));
        homeMain.atv_home_main.setText(moduleDo.getName());
        return convertView;
    }

    class HomeMain {
        public ImageView img_home_main;
        public AppCompatTextView atv_home_main;
    }
}
